package com.woke.method;
//
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.util.Log;

import com.http.HttpMethods;


public class UploadImage {  
	public String upload(ArrayList<String> uploadFiles, String name, String money,
			String content) {
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		String xmString = "";  
		String xmlUTF8=""; 
		String xmString1 = "";  
		String xmlUTF81=""; 
		try {
			xmString = new String(name.getBytes("UTF-8"));
			xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");  
			xmString1 = new String(content.getBytes("UTF-8"));
			xmlUTF81 = URLEncoder.encode(xmString, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}  
		try {
			URL url = new URL(HttpMethods.BASEURL+"g=api&m=user&a=additem"+"&money="+money+
					"&title="+xmlUTF8+"&content="+xmlUTF81);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);
			con.setRequestMethod("POST");
			con.setRequestProperty("Connection", "Keep-Alive");
			con.setRequestProperty("Charset", "UTF-8");
			con.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			DataOutputStream ds = new DataOutputStream(con.getOutputStream());
			for (int i = 0; i < uploadFiles.size(); i++) {
				String uploadFile = uploadFiles.get(i);
				String filename = uploadFile.substring(uploadFile
						.lastIndexOf("\\") + 1);
				Log.e("filename", filename);
				ds.writeBytes(twoHyphens + boundary + end);
				// ds.writeBytes("Content-Disposition: form-data; "+"name=\"file"
				// + i + "\";filename=\""+filename + "\"" + end);
				ds.writeBytes("Content-Disposition: form-data; "+ "name=\"file" + (i + 1) + "\";filename=\""+ "zhongcai" + i + ".jpg" + "\"" + end);
				ds.writeBytes(end);
				
				Mapupload maplos = new Mapupload();
				Bitmap getimage = maplos.getimage(uploadFile,100);
				InputStream fStream = maplos.Bitmap2IS(getimage);
				int bufferSize = 1024;
				byte[] buffer = new byte[bufferSize];
				int length = -1;
				while ((length = fStream.read(buffer)) != -1) {
					ds.write(buffer, 0, length);
				}
				ds.writeBytes(end);
				fStream.close();
			}
			ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
			ds.flush();
			InputStream is = con.getInputStream();
			int ch;
			StringBuffer b = new StringBuffer();
			while ((ch = is.read()) != -1) {
				b.append((char) ch);
			}
			String s = b.toString();
			JSONObject json = new JSONObject(s);
			if(null != json.getString("flag") ){
				String string = json.getString("flag");
				if(string.equals("success")){
					JSONObject jsonObject = json.getJSONObject("data");
					JSONObject jsonObject2 = jsonObject.getJSONObject("data");
					String id = jsonObject2.getString("id");
					return id;
					}else{
						return "false";
					}		
				}
			ds.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		}
	
	public String uploadda(ArrayList<String> uploadFiles, String name, String money,
			 String time,String content, String id, int zhongtype, String userid) {
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		String xmString = "";  
		String xmlUTF8=""; 
		String xmString1 = "";  
		String xmlUTF81=""; 
		try {
			xmString = new String(name.getBytes("UTF-8"));
			xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");  
			xmString1 = new String(content.getBytes("UTF-8"));
			xmlUTF81 = URLEncoder.encode(xmString1, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}  
		try {
			URL url = new URL(HttpMethods.BASEURL+"g=api&m=user&a=addcrowd"+"&money="+money+"&product_name="+xmlUTF8+
					"&content="+xmlUTF81+"&end_time="+time+"&type="+zhongtype+"&userid="+userid+"&itemid="+id);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);
			con.setRequestMethod("POST");
			con.setRequestProperty("Connection", "Keep-Alive");
			con.setRequestProperty("Charset", "UTF-8");
			con.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			DataOutputStream ds = new DataOutputStream(con.getOutputStream());
			for (int i = 0; i < uploadFiles.size(); i++) {
				String uploadFile = uploadFiles.get(i);
				String filename = uploadFile.substring(uploadFile
						.lastIndexOf("\\") + 1);
				Log.e("filename", filename);
				ds.writeBytes(twoHyphens + boundary + end);
				// ds.writeBytes("Content-Disposition: form-data; "+"name=\"file"
				// + i + "\";filename=\""+filename + "\"" + end);
				ds.writeBytes("Content-Disposition: form-data; "+ "name=\"file" + (i + 1) + "\";filename=\""+ "zhongcai" + i + ".jpg" + "\"" + end);
				ds.writeBytes(end);
				
				Mapupload maplos = new Mapupload();
				Bitmap getimage = maplos.getimage(uploadFile,100);
				InputStream fStream = maplos.Bitmap2IS(getimage);
				int bufferSize = 1024;
				byte[] buffer = new byte[bufferSize];
				int length = -1;
				while ((length = fStream.read(buffer)) != -1) {
					ds.write(buffer, 0, length);
				}
				ds.writeBytes(end);
				fStream.close();
			}
			ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
			ds.flush();
			InputStream is = con.getInputStream();
			int ch;
			StringBuffer b = new StringBuffer();
			while ((ch = is.read()) != -1) {
				b.append((char) ch);
			}
			String s = b.toString();
			JSONObject json = new JSONObject(s);
			Log.e("json", "json"+json.toString());
			if(null != json.getString("flag") ){
				String string = json.getString("flag");
				if(string.equals("success")){
					return "true";
					}else{
						return "false";
					}		
				}
			ds.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		}
	
	}

